package com.roger.tool;

import android.content.Context;
import android.text.TextUtils;

import com.roger.tool.json.KJSONObject;
import com.roger.tool.net.SGHttpTask;
import com.roger.tool.net.SGHttpTask.SGHttpListener;

public class CityUtil {
	//
	// 1、新浪IP地址查询API接口
	// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=IP地址
	// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=115.156.238.114
	// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
	// 返回：var remote_ip_info =
	// {"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u6df1\u5733","district":"","isp":"","type":"","desc":""};
	//
	// 2、淘宝：
	// http://ip.taobao.com/service/getIpInfo.php?ip=myip
	// 返回：{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5357","area_id":"800000","region":"\u5e7f\u4e1c\u7701","region_id":"440000","city":"\u6df1\u5733\u5e02","city_id":"440300","county":"\u798f\u7530\u533a","county_id":"440304","isp":"\u7535\u4fe1","isp_id":"100017","ip":"61.141.156.62"}}
	// http://ip.taobao.com/service/getIpInfo.php?ip=115.156.238.114
	//
	// 3、搜狐：
	// http://pv.sohu.com/cityjson
	// http://pv.sohu.com/cityjson?ie=utf-8
	// 返回：var returnCitySN = {"cip": "218.17.161.2", "cid": "440300", "cname":
	// "广东省深圳市"};

	private static final String URL_TAOBAO = "http://ip.taobao.com/service/getIpInfo.php?ip=myip";
	private static final String URL_SOHU = "http://pv.sohu.com/cityjson?ie=utf-8";
	private static final String URL_SINA = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";

	public static final String Shenzhen = "深圳";
	public static final String Guangzhou = "广州";
	public static final String Shanghai = "上海";
	public static final String Beijing = "北京";

	public static void locateCityTaobao(final Context pContext, final IOnGetCityListener pOnGetCityListener) {

		/*
		 * 1. 请求接口（GET）： /service/getIpInfo.php?ip=[ip地址字串] 2. 响应信息： （json格式的）国家
		 * 、省（自治区或直辖市）、市（县）、运营商 3. 返回数据格式：
		 * {"code":0,"data":{"ip":"210.75.225.254"
		 * ,"country":"\u4e2d\u56fd","area":"\u534e\u5317",
		 * "region":"\u5317\u4eac\u5e02"
		 * ,"city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
		 * "country_id"
		 * :"86","area_id":"100000","region_id":"110000","city_id":"110000",
		 * "county_id":"-1","isp_id":"100017"}} 其中code的值的含义为，0：成功，1：失败。
		 */
		SGHttpTask mSGHttpTask = new SGHttpTask(pContext);

		String url = URL_TAOBAO;

		mSGHttpTask.doGet(new SGHttpListener() {

			@Override
			public void onResponse(String response) {
				SGLog.v("CityUtil-->onResponse=" + response);
				String city = null;
				try {
					KJSONObject kjsonObject = KJSONObject.createJsonObject(response);
					if (kjsonObject != null) {
						String code = kjsonObject.getString("code", "1");
						if ("0".equals(code)) {
							KJSONObject data = kjsonObject.getJSONObject("data");
							city = data.getString("city");
						}
					}
				} catch (Exception e) {

				}

				if (!TextUtils.isEmpty(city)) {
					pOnGetCityListener.onGetCity(city);
				} else {
					// 获取失败 下一个方式
				}
			}

			@Override
			public void onCancelled() {
			}
		}, url);
	}

	public static void locateCitySohu(final Context pContext, final IOnGetCityListener pOnGetCityListener) {

		SGHttpTask mSGHttpTask = new SGHttpTask(pContext);

		String url = URL_SOHU;

		mSGHttpTask.doGet(new SGHttpListener() {

			@Override
			public void onResponse(String response) {
				SGLog.v("CityUtil-->onResponse=" + response);
				String city = null;
				try {
					KJSONObject kjsonObject = KJSONObject.createJsonObject(response);
					// {"cip": "61.141.156.62", "cid": "440304", "cname":
					// "广东省深圳市福田区"};
					if (kjsonObject != null) {
						city = kjsonObject.getString("cname", "");
					}
				} catch (Exception e) {

				}

				if (!TextUtils.isEmpty(city)) {
					pOnGetCityListener.onGetCity(city);
				} else {
					// 获取失败 下一个方式
				}
			}

			@Override
			public void onCancelled() {
			}
		}, url);
	}

	public static void locateCitySina(final Context pContext, final IOnGetCityListener pOnGetCityListener) {

		SGHttpTask mSGHttpTask = new SGHttpTask(pContext);

		String url = URL_SINA;

		mSGHttpTask.doGet(new SGHttpListener() {

			@Override
			public void onResponse(String response) {
				SGLog.v("CityUtil-->onResponse=" + response);
				String city = null;
				try {
					KJSONObject kjsonObject = KJSONObject.createJsonObject(response);
					// {"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u6df1\u5733","district":"","isp":"","type":"","desc":""};
					if (kjsonObject != null) {
						city = kjsonObject.getString("city", "");
					}
				} catch (Exception e) {

				}

				if (!TextUtils.isEmpty(city)) {
					pOnGetCityListener.onGetCity(city);
				} else {
					// 获取失败 下一个方式
				}
			}

			@Override
			public void onCancelled() {
			}
		}, url);
	}

	public interface IOnGetCityListener {

		public void onGetCity(String pCityName);

	}
}
